﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Argotic.Common;
using Argotic.Syndication;
using Argotic.Extensions;
using System.Collections.Generic;
using Argotic.Extensions.Core;
using System.Text.RegularExpressions;

namespace Linkmeme.Utility
{
    public class FeedReader
    {
        

        public static List<Bookmark> GetBookmark(string url, string providerName)
        {
            RssFeed feed = RssFeed.Create(new Uri(url));
            
            var bm = (from f in feed.Channel.Items
                     select BuildBookmarkObject(f, providerName)).ToList();
            return bm;
        }

        public static List<Picture> GetPhoto(string rssUrl, string displayName)
        {
            RssFeed feed = RssFeed.Create(new Uri(rssUrl));
            var bm = (from f in feed.Channel.Items
                      select BuildPictureObject(f, displayName)).ToList();
            return bm;
        }

        public static List<Blog> GetBlog(string url, string providerName)
        {
            RssFeed feed = RssFeed.Create(new Uri(url));
            var blog = (from f in feed.Channel.Items
                      select new Blog
                      {
                          ProviderName = providerName,
                          Title = f.Title,
                          PubDate = f.PublicationDate,
                          Link = f.Link.ToString(),
                          Description = f.Description,
                          Author = f.Author,
                          UserName = f.Author,
                          Tags = "",
                          Type = f.Categories.ToString()
                      }).ToList();
            return blog;
        }

        public static List<Status> GetStatus(string RssUrl, string DisplayName)
        {
            RssFeed feed = RssFeed.Create(new Uri(RssUrl));
            var status = (from f in feed.Channel.Items
                        select new Status
                        {
                            ProviderName = DisplayName,
                            Title = f.Title,
                            PubDate = f.PublicationDate,
                            Link = f.Link.ToString(),
                            Description = f.Description,
                            UserName = "", //f.Author,
                            Tags = "",
                            Type = ""   //f.Categories.ToString()
                        }).ToList();
            return status;
        }


        private static Bookmark BuildBookmarkObject(RssItem ri, string providerName)
        {
            Bookmark bm =new Bookmark() {
                         ProviderName = providerName,
                         Title = ri.Title,
                         Link = ri.Link.ToString(),
                         Description = ri.Description,
                         
                         PubDate = ri.PublicationDate,
                         UserName = ri.Author,

                         Tags = "",
                         Type = ri.Categories.ToString()
                     };

            if (ri.HasExtensions)
            {
                DublinCoreElementSetSyndicationExtension itemDCExtension = ri.FindExtension(DublinCoreElementSetSyndicationExtension.MatchByType) as DublinCoreElementSetSyndicationExtension;
                if (itemDCExtension != null)
                {
                    bm.UserName = itemDCExtension.Context.Creator;
                    bm.PubDate = itemDCExtension.Context.Date;
                }
            }

            return bm;
        }


        private static Picture BuildPictureObject(RssItem ri, string displayName)
        {
            Picture pic = new Picture()
            {
                ProviderName = displayName,
                UserName = ri.Author,
                Link = ri.Link.ToString(),
                PubDate = ri.PublicationDate,
                Url = ri.GetPhotoUrl(displayName), 
                Type = ri.Categories.ToString(),
                Width = 50,
                Height = 50   
            };

            if (ri.HasExtensions)   //for yupoo via yahoopipes
            {
                YahooMediaSyndicationExtension itemExtension = ri.FindExtension(YahooMediaSyndicationExtension.MatchByType) as YahooMediaSyndicationExtension;
                if (itemExtension != null)
                {
                    pic.Url = itemExtension.Context.Thumbnails.First().Url.ToString();
                    pic.Width = itemExtension.Context.Thumbnails.First().Width;
                    pic.Height = itemExtension.Context.Thumbnails.First().Height;
                    pic.UserName = itemExtension.Context.Credits.First().ToString();
                    pic.Type = itemExtension.Context.Categories.First().ToString();
                }
            }

            return pic;
        }



       
    }


   
}
